package qgs.zisu.javaconcurrency.javaconcurrency.chapter12_ThreadGroup;

import java.util.Arrays;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/20 19:50
 * @Version 1.0
 */
public class ThreadGroupRelate {
    public static void main(String[] args) {
        //use the name
        ThreadGroup tg1 = new ThreadGroup("TG1");
        Thread t1 = new Thread(tg1,"t1"){
            public boolean flag = true;
            @Override
            public void run(){
                while (flag){
                    try {
                        System.out.println("getName(in)"+getThreadGroup().getName());
                        System.out.println("getParent(in)"+getThreadGroup().getParent());
                        System.out.println("activeCount(in)"+getThreadGroup().activeCount());
                        Thread.sleep(1_000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    flag = false;
                }
            }
        };
        t1.start();

        ThreadGroup tg2 = new ThreadGroup("TG2");
        Thread t2 = new Thread(tg2,"t2"){
            public boolean flag = true;
            @Override
            public void run(){
                // 同辈之间都是能够访问的，还能访问后辈
                System.out.println(tg1.getName());
                //列出 tg1 活跃的线程数
                Thread[] threads = new Thread[tg1.activeCount()];
                tg1.enumerate(threads);

                Arrays.asList(threads).forEach(System.out::println);

            }
        };
        t2.start();
    }
}
